﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class TopicEdit:TopicBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected string oldTagStr = null;
        protected void HandleCommon()
        {
            this.AddLocation("修改帖子", "/BBS/TopicEdit.aspx");
            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击先登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }
            //判断权限
            //if (UserData.Id != TopicData.PostUserId)
            if(!CanEdit(TopicData))
            {
                this.AddErrLine("你没有权限操作！");
                return;
            }
            UserInfo _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            if ((_User.UserTypeId & 0x100) != 0)
            {
                this.AddErrLine("您已被在本站禁止发言");
                return;
            }
            oldTagStr = TopicData.Tags;

        }

        protected bool CanEdit(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }
            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }
            return retVal;
        }
        protected bool CompareArrValue(string str, string[] arrayValue)
        {
            if (arrayValue.Length < 0)
            {
                return false;
            }
            for (int i = 0; i < arrayValue.Length; i++)
            {
                if (str == arrayValue[i])
                {
                    return true;
                }
            }
            return false; 
        }
        protected void HandlePost()
        {
            string[] tagArrNew = null;
            string[] tagArrOld = null;
            string[] tempTagArr = null;
            List<string> tagArrAdd = new List<string>();
            List<string> tagArrDel = new List<string>();
            TopicInfo topic = ValidateForm();
            if (ErrorCount > 0) return;
            tagArrNew = Regex.Split(topic.Tags, "\\s+");
            tagArrOld = Regex.Split(oldTagStr, "\\s+");
            if (tagArrNew.Length > 0)
            {
                if (tagArrNew[0] != "")
                {
                    for (int i = 0; i < tagArrNew.Length; i++)
                    {
                        if (!CompareArrValue(tagArrNew[i], tagArrOld))
                        {
                            tagArrAdd.Add(tagArrNew[i]);
                        }
                    }
                }
            }
            if (tagArrOld.Length > 0)
            {
                if (tagArrOld[0] != "")
                {
                    for (int j = 0; j < tagArrOld.Length; j++)
                    {
                        if (!CompareArrValue(tagArrOld[j], tagArrNew))
                        {
                            tagArrDel.Add(tagArrOld[j]);
                        }
                    }
                }
            }
            if (tagArrAdd.Count > 0)
            {
                tempTagArr = tagArrAdd.ToArray();
                if (tempTagArr.Length > 0)
                {
                    if (tempTagArr[0] != "")
                    {
                        TagBLL.Instance.CreateTopicTag(tempTagArr, 1, topic.Id, topic.PostUserId);
                    }
                }
            }
            if (tagArrDel.Count > 0)
            {
                tempTagArr = tagArrDel.ToArray();
                if (tempTagArr.Length > 0)
                {
                    if (tempTagArr[0] != "")
                    {
                        TagBLL.Instance.DelTopicTag(tempTagArr, 1, topic.Id);
                    }
                }
            }

            bool succ = TopicBLL.Instance.UpdateTopic(topic);
            if (succ)
            {
                this.Redirect(string.Format("/BBS/Topic.aspx?gid={0}&tid={1}", GroupData.Id,TopicData.Id));
            }
            else
            {
                this.AddErrLine("操作失败！");
                return;
            }
        }


        protected void HandleGet()
        {
        }

        TopicInfo ValidateForm()
        {
            TopicInfo info = new TopicInfo();
            info = TopicData;
            info.Title = RequestTools.GetString("ttitle").Trim();
            info.Body = RequestTools.GetString("tbxContent").Trim();
            info.Tags = RequestTools.GetString("tags").Trim();
            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能空！");
                return null;
            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过"+Config.TitleMaxSize+"字符！");
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空！");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能大于 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            if (info.Tags.Length > 120)
            {
                this.AddErrLine("所有标签总长度不能大于120字符！");
                return null;
            }
            info.UpdateTime = DateTime.Now;
            //info.PostUserId = UserData.Id;
            info.Groupid = GroupData.Id;
            info.Id = TopicData.Id;

            return info;
        }
    }
}